package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> ULogin(HttpServletRequest request, @RequestBody Employee employee){
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据用户名查询
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //3
        if (password==null){
            return R.error("登录失败");
        }
        //4
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        //5
        if (emp.getStatus()==0){
            return R.error("账号已经禁止使用");
        }
        //6
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R<Page> selectPage(HttpServletRequest request, int page, int pageSize,String name){
        //分页构造器
        Page page2 = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //弄一个模糊查询
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //加一个排序
        wrapper.orderByDesc(Employee::getUpdateTime);
        // 条件分页查询
        //IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
        employeeService.page(page2,wrapper);
        return R.success(page2);
    }
    @PostMapping
    public R<String> addJson(HttpServletRequest request,@RequestBody Employee employee){
        //将新增的用户的密码进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置一下时间，用来时间排序
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id        ----------不知道干啥用的
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增成功");
    }
    @PutMapping
    public R<String> updateById(HttpServletRequest request,@RequestBody Employee employee){
        /*Long empId = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        // 根据 ID 修改
        //boolean updateById(T entity);
        employeeService.updateById(employee);
        return R.success("员工状态操作成功");
    }

    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        // 根据 ID 查询
        //T getById(Serializable id);
        Employee byId = employeeService.getById(id);
        if (byId!=null){
            return R.success(byId);
        }
        return R.error("没查询到这个员工");
    }

}
